/*
* @Author: Black / 李来然
* @Date: 2021-12-09 11:53:56
* @LastEditors: Black / 李来然
* @LastEditTime: 2022-02-13 13:48:32
* @Description: 通用模块
*/
import { ModsInstance } from "@/func/createMod";
import { ModeEvent } from "../tools/ModeEvent";
import { getModeFuncRet, RetResult } from "../tools/modHelper";

export interface CommonState { }
export interface CommonCapability { }

type WriteFileAppendRet = "FileWrited" | "WriteFileFail" | "FatalError";

export type DirStruct = {
	file: string[]
} | null


export type FomatterList = {
	printerName: string
}

export type FomatterQuene = {
	job_name: string
}

export type PrinterListRet = {
	printer_default: string
	printer_list: FomatterList[]
}

export type PrintQueneRet = {
	printer_status: string
	printer_jobs: FomatterQuene[]
}

export interface MACINFO {
	MACINFO: Macinfo[];
}

export interface Macinfo {
	ADAPTERNAME: string;
	DESCRIPTION: string;
	IPARRAY:     Iparray[];
	MACADDRESS:  string;
}

export interface Iparray {
	IP: string;
}
export class Common extends ModeEvent {
	constructor(modName: keyof ModsInstance, mod: any) {
		super(modName, mod);
	}

	/**
	 *  获取BASE64格式的图片数据
	 *  @param    strFile  文件名
	 *  @return   BASE64   数据
	 */
	ReadImageBase64(strFile: string) {
		Trace(EC_INFO, "-->Common:readImageBase64");
		this.setCurrentMethod("ReadImageBase64");
		this.removeMethodBindEvt("ReadImageBase64");
		return new Promise<string>((resolve, reject) => {
			this.mod.GetImgBase64(strFile, (ret: string) => {
				resolve(ret);
			});
		}).then((value) => {
			this.removeMethodBindEvt("ReadImageBase64");
			return value;
		});
	}

	/**
	 * 删除文件
	 * @param {string} path 删除文件名含路径
	 * @return Promise
	 */
	DeleteFile(path: string) {
		Trace(EC_INFO, "-->Common:DeleteFile");
		return new Promise(resolve => {
			this.mod.DeleteFile(path, (ret: string) => {
				Trace(EC_INFO, "<--Common:DeleteFile ret=" + ret);
				resolve(parseInt(ret));
			});
		})
	}

	/**
	 * 写入文件到磁盘中去
	 */
	WriteFileAppend(strFilePath: string, strData: string) {
		Trace(EC_INFO, "-->Common:WriteFileAppend");
		this.setCurrentMethod("WriteFileAppend");
		this.removeMethodBindEvt("WriteFileAppend");
		return new Promise<RetResult<WriteFileAppendRet>>((resolve, reject) => {
			const FileWrited = (...args: any[]) => {
				Trace(EC_INFO, "<--Common:WriteFileAppend FileWrited");
				resolve(getModeFuncRet(this.modName, "FileWrited", args));
			};
			const WriteFileFail = (...args: any[]) => {
				Trace(EC_INFO, "<--Common:WriteFileAppend WriteFileFail");
				resolve(getModeFuncRet(this.modName, "WriteFileFail", args));
			};

			this.add("FileWrited", FileWrited);
			this.add("WriteFileFail", WriteFileFail);

			this.mod.WriteFileAppend(strFilePath, strData, (ret: string) => {
				if (parseInt(ret) !== 0) {
					Trace(EC_INFO, "<--Crd:Reset ret=" + ret);
					resolve(getModeFuncRet(this.modName, "FatalError"));
				}
			});
		}).then((value) => {
			this.removeMethodBindEvt("WriteFileAppend");
			return value;
		});
	}

	/**
	 * 读取文件
	 */
	ReadFile(strFilePath: string) {
		Trace(EC_INFO, "-->Common:ReadFile");
		return new Promise(resolve => {
			this.mod.ReadFile(strFilePath, (ret: string) => resolve(parseInt(ret)));
		})
	}

	WriteFile(strFilePath: string, strData: string | Blob, strType: string, offest: string) {
		Trace(EC_INFO, "-->Common:WriteFile");
		return new Promise(resolve => {
			this.mod.WriteFile(strFilePath, strData, strType, offest, (ret: string) => resolve(parseInt(ret)));
		})
	}

	/**
	 * 生成二维码
	 */
	GenBarcodeImage(
		strBarcode: string,
		strFileName: string,
		short: number,
		sHeight: number,
		strType: string
	) {
		Trace(EC_INFO, "-->Common:GenBarcodeImage");
		this.setCurrentMethod("GenBarcodeImage");
		this.removeMethodBindEvt("GenBarcodeImage");
		this.mod.GenBarcodeImage(strBarcode, strFileName, short, sHeight, strType);
	}

	/**
	 * 读取ini文件
	 */
	ReadInI(
	) {
		Trace(EC_INFO, "-->Common:ReadInI");
		this.setCurrentMethod("ReadInI");
		this.removeMethodBindEvt("ReadInI");
		return new Promise<string>((resolve, reject) => {
			this.mod.readini('C:\\eastcom\abwoa\bin\faceStandard.ini', 'FACE', 'standard', 60, (ret: string) => {
				Trace(EC_INFO, "<--Common:ReadInI ret=" + ret);
				resolve(ret);
			});
		}).then((value) => {
			this.removeMethodBindEvt("ReadInI");
			return value;
		});
	}

	/**
	 * 判断文件夹是否存在
	 * @param {string} strpath - 文件夹路径
	 */
	IsPathFileExists(strPath: string) {
		Trace(EC_INFO, "-->Common:IsPathFileExists");
		return new Promise<number>((resolve, reject) => {
			this.mod.IsPathFileExists(strPath, (ret: string) => {
				Trace(EC_INFO, `<--Common:IsPathFileExists ret=${ret}`);
				resolve(parseInt(ret));
			});
		})
	}
	IsFileExist(strPath: string) {
		Trace(EC_INFO, "-->Common:IsPathFileExists");
		return new Promise<number>((resolve, reject) => {
			this.mod.IsFileExist(strPath, (ret: string) => {
				Trace(EC_INFO, `<--Common:IsPathFileExists ret=${ret}`);
				resolve(parseInt(ret));
			});
		})
	}

	/**
	 * 创建文件夹
	 * @param {string} strDir - 创建的文件夹名字
	 */
	CreateDir(strDir: string) {
		Trace(EC_INFO, "-->Common:CreateDir");
		this.setCurrentMethod("CreateDir");
		this.removeMethodBindEvt("CreateDir");
		return new Promise<number>((resolve, reject) => {
			this.mod.CreateDir(strDir, (ret: number) => {
				Trace(EC_INFO, `<--Common:CreateDir ret=${ret}`);
				resolve(ret);
			});
		}).then((value) => {
			this.removeMethodBindEvt("CreateDir");
			return value;
		});
	}

	/**
	 * 遍历文件夹，返回文件结构
	 */
	TraversalDir(strPath: string) {
		Trace(EC_INFO, "-->Common:TraversalDir");
		this.setCurrentMethod("TraversalDir");
		this.removeMethodBindEvt("TraversalDir");
		return new Promise<string | null>((resolve, reject) => {
			this.mod.TraversalDir(strPath, (ret: string | null) => {
				Trace(EC_INFO, `<--Common:TraversalDir ret=${ret}`);
				resolve(ret);
			});
		}).then((value) => {
			this.removeMethodBindEvt("TraversalDir");
			return value;
		});
	}
	CopyFileToDes(strPath: string, strDesFileName: string) {
		Trace(EC_INFO, "-->Common:CopyFileToDes");
		return new Promise<number>((resolve, reject) => {
			this.mod.CopyFileToDes(strPath, strDesFileName, (ret: string) => {
				Trace(EC_INFO, `<--Common:CopyFileToDes ret=${ret}`);
				resolve(parseInt(ret));
			});
		})
	}
	/**
	 *         返回json对象字符串；定义如下:
	 * { "printer_default":"默认打印机名称", "printer_list" : [ 名称1，...]  }
	 */
	GetPrinterNames() {
		Trace(EC_INFO, "-->Common:GetPrinterNames");
		return new Promise<any>((resolve, reject) => {
			this.mod.GetPrinterNames((ret: any) => {
				Trace(EC_INFO, `<--Common:GetPrinterNames ret=${ret}`);
				let data = JSON.parse(ret) 
				resolve(data);
			});
		})
	}

	/**
		* 入参: strPrinter(打印机名称)
		* 返回JSON对象字符串
		* Json结构 { "printer_status" : "打印机状态",  "printer_jobs":[ { "job_name":"任务名称", "job_status":"任务状态" }, ... ]  }
		*
	*/
	GetPrinterJobsByName(strPrinter: string) {
		Trace(EC_INFO, "-->Common:GetPrinterJobsByName");
		return new Promise<any>((resolve, reject) => {
			this.mod.GetPrinterJobsByName(strPrinter, (ret: PrintQueneRet) => {
				Trace(EC_INFO, `<--Common:GetPrinterJobsByName ret=${ret}`);
				resolve(ret);
			});
		})
	}

	PrintPdf(pdfFile: string, printerName: string, direction: number) {
		Trace(EC_INFO, "-->Common:PrintPdf");
		console.log(pdfFile);
		console.log(printerName);
		console.log(direction);
		
		return new Promise<number>((resolve, reject) => {
			this.mod.PrintPdf(pdfFile, printerName, direction, (ret: number) => {
				Trace(EC_INFO, `<--Common:PrintPdf ret=${ret}`);
				resolve(ret);
			});
		})
	}

	GetNetworkInfo() {
		Trace(EC_INFO, "-->Common:GetNetworkInfo");
		return new Promise<MACINFO>((resolve, reject) => {
			this.mod.GetMacInfo((ret: string) => {
				Trace(EC_INFO, `<--Common:GetNetworkInfo ret=${ret}`);
				resolve(JSON.parse(ret));
			});
		})
	}
}
